<template>
	<view>
		nfc读写
	</view>
</template>

<script>
	export default {
		data() {
			return {
				nfcAdapter: null
			}
		},
		onLaunch() {
			// console.log(111);
			// this.initNFC();
			console.log('onLaunch');
		},
		onLoad() {
			console.log('onLoad');
			this.initNFC();
		},
		methods: {
			// 初始化 NFC 适配器
			initNFC() {
				const nfcAdapter = wx.getNFCAdapter();
				nfcAdapter.startDiscovery({
					success: () => {
						console.log('NFC discovery started');
						uni.showToast({
							title: 'NFC 启动成功',
							icon: 'none'
						});
					},
					fail: (err) => {
						console.error('Failed to start NFC discovery:', err);
						uni.showToast({
							title: '启动 NFC 失败',
							icon: 'none'
						});
					}
				});
				// 读取
				// nfcAdapter.onDiscovered((res) => {
				// 	console.log('NFC Tag Discovered:', res);
				// 	uni.showToast({
				// 		title: '发现 NFC 标签',
				// 		icon: 'none'
				// 	});
				// 	console.log('nfc类型:', res.techs);
				// 	if (res.techs.includes('NFC-A')) {
				// 		console.log('NFC-A Tag Detected');
				// 		this.connectAndReadTag(nfcAdapter, res.techs[0]);
				// 	} else {
				// 		console.error('Unsupported NFC tag type');
				// 		uni.showToast({
				// 			title: '不支持的标签类型',
				// 			icon: 'none'
				// 		});
				// 	}
				// });
				// 写入
				nfcAdapter.onDiscovered((res) => {
					console.log('Tag discovered:', res);

					if (res.techs.includes('NFC-A')) {
						console.log('Found NFC-A tag, ready to write.');
						const url = 'https://miniapp.example.com'; // 小程序地址
						// const dataArray = this.stringToBytes(url);  // 将字符串转为字节数组
						// this.writeUrlData(dataArray);  // 写入数据
						this.writeNDEFUrl(url,nfcAdapter)
					} else {
						console.error('Unsupported tag type.');
						uni.showToast({
							title: '不支持的标签类型',
							icon: 'none'
						});
					}
				});
			},
			// 将字符串转换为字节数组
			 stringToBytes(str) {
			  const bytes = [];
			  for (let i = 0; i < str.length; i++) {
			    bytes.push(str.charCodeAt(i));
			  }
			  return new Uint8Array(bytes);
			},

			// 将数据分段写入 NFC 标签，每次写 4 字节
			writeNDEFUrl(url,nfcAdapter) {
				const message = [{
					tnf: 1, // TNF_WELL_KNOWN
					type: new Uint8Array([0x55]), // 'U' 表示 URL 类型
					payload: this.stringToBytes(url) // URL 转为字节数组
				}];

				nfcAdapter.writeNDEFMessage({
					message,
					success: () => {
						console.log('NDEF message written successfully');
						uni.showToast({
							title: '写入成功',
							icon: 'none'
						});
					},
					fail: (err) => {
						console.error('Failed to write NDEF message:', err);
						uni.showToast({
							title: '写入失败',
							icon: 'none'
						});
					}
				});
			},
			// 连接并读取 NFC 标签
			connectAndReadTag(nfcAdapter, tech) {

				nfcAdapter.transceive({
					data: new Uint8Array([0x30, 0x04]).buffer, // 示例：Mifare 读取命令
					success: (res) => {
						const data = new Uint8Array(res.data);
						console.log('NFC Data:', data);
						uni.showToast({
							title: '读取数据成功',
							icon: 'none'
						});
					},
					fail: (err) => {
						console.error('Failed to read data:', err);
						uni.showToast({
							title: '读取数据失败',
							icon: 'none'
						});
					}
				});

			},
			// 读取 NFC 标签的数据
		},

	}
</script>

<style>

</style>